﻿using Microsoft.EntityFrameworkCore;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Infrastructure.Common;

namespace WMS.FC.Infrastructure.Repository
{
    public class StationRepository : RepositoryBase, IStationRepository
    {

        public StationRepository(WmsDbContext dbContext) : base(dbContext)
        {
        }

        public async Task<StationPO?> GetStation(string lineCode, string stationCode)
        {
            return await dbContext.Set<StationPO>().AsNoTrackingWithIdentityResolution().FirstOrDefaultAsync(entity => entity.LineCode == lineCode && entity.Code == stationCode);
        }

        public async Task<List<EquipmentPO>> GetToEquipmentList(string lineCode, string stationCode)
        {
            var stationRouteList = await dbContext.Set<StationRoutePO>().AsNoTrackingWithIdentityResolution().Where(entity => entity.LineCode == lineCode && entity.StationCode == stationCode).ToListAsync();
            var equipmentCodeList = stationRouteList.Where(entity => !string.IsNullOrWhiteSpace(entity.ToEquipmentCode)).Select(entity => entity.ToEquipmentCode).ToList();
            return await dbContext.Set<EquipmentPO>().AsNoTrackingWithIdentityResolution().Where(entity => entity.LineCode == lineCode && equipmentCodeList.Contains(entity.Code)).ToListAsync();
        }

        public async Task<List<StationPO>> GetToStationList(string lineCode, string stationCode)
        {
            var stationRouteList = await dbContext.Set<StationRoutePO>().AsNoTrackingWithIdentityResolution().Where(entity => entity.LineCode == lineCode && entity.StationCode == stationCode).ToListAsync();
            var stationCodeList = stationRouteList.Where(entity => !string.IsNullOrWhiteSpace(entity.ToStationCode)).Select(entity => entity.ToStationCode).ToList();
            return await dbContext.Set<StationPO>().AsNoTrackingWithIdentityResolution().Where(entity => entity.LineCode == lineCode && stationCodeList.Contains(entity.Code)).ToListAsync();
        }
    }
}
